/**
 * Created by BINGO on 2016/10/31.
 */
var zTreeObj;

var layer2;//定义选择框弹框

// zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "pid",
            rootPId: null
        },
        key: {
            name: "name",
            url: "_url"
        }
    },
    callback: {
        onClick: function (event, treeId, treeNode) {
            $("#_pid").val(treeNode.id);
            $("#_thisSel").val(treeNode.name);
            layer.close(layer2)
        }
    }
};

//增加或修改资源弹框
var resEdit = function (id) {
    var layer1 = layer.open({
        type: 0,
        title: '<i class="fa fa-pencil m-r-5"></i>编辑资源',
        area: ['500px', '520px'], //宽高
        btn: ['确定'],
        zIndex: 1,
        scrollbar: false,//禁止浏览器滚动条
        content: '<div id="_resAdd"></div>', //注意，如果str是object，那么需要字符拼接。
        yes: function (index, layero) {
            $.ajax({
                type: "POST",
                async: false,
                data: $('#_postRes').serialize(),
                url: "/resource/save",
                success: function (msg) {
                    layer.close(layer1);
                    var el = document.createElement("a");
                    document.body.appendChild(el);
                    el.href = "#/resource/?time=" + new Date().getTime(); //url 是你得到的连接
                    //el.target = '_blank'; //指定在新窗口打开
                    el.click();
                    document.body.removeChild(el);
                },
                error: function (msg) {
                }
            });
        }
    });

    //获取页面内容
    $.ajax({
        url: "/resource/editPage",
        dataType: "html",
        data: {id: id},
        success: function (result) {
            $("#_resAdd").append(result)
        }
    })
}

//下拉树弹框
var getSelectTree = function () {
    layer2 = layer.open({
        title: '请选择上级资源',
        area: ['300px', '500px'], //宽高
        zIndex: 2,
        type: 1,
        scrollbar: false,
        content: '<ul id="sel_tree" class="ztree">'
    })

    $.ajax({
        url: "/resource/getSelectTree",
        dataType: "json",
        success: function (result) {
            zTreeObj = $.fn.zTree.init($("#sel_tree"), setting, result);
            zTreeObj.expandAll(true);
        }
    })
}

//增加或修改资源弹框
var resDel = function (id) {
    var layer3 = layer.confirm('删除时同时会删除下级所有子节点？', {
        btn: ['确认', '取消'] //按钮
    }, function () {
        $.ajax({
            url: "/resource/delete",
            data: {id: id},
            success: function (msg) {
                layer.close(layer3);
                var el = document.createElement("a");
                document.body.appendChild(el);
                el.href = "#/resource/?time=" + new Date().getTime(); //url 是你得到的连接
                //el.target = '_blank'; //指定在新窗口打开
                el.click();
                document.body.removeChild(el);
            }
        });
    }, function () {
        layer.close(layer3)
    });
}

var SysResource = function () {
    "use strict";
    return {
        //main function
        init: function () {
            $('.tree').treegrid();
        }
    };
}();